package com.wdb.common.configurer.subject;


import com.wdb.common.constant.Constant;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public abstract class SubjectContext {
    private static final ThreadLocal<Map<String, Object>> LOCAL = new ThreadLocal<>();

    public static void set(String key, Object value) {
        Map<String, Object> cache = LOCAL.get();
        if (Objects.isNull(cache)) {
            cache = new HashMap<>();
            LOCAL.set(cache);
        }
        cache.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> cache = LOCAL.get();
        if (Objects.isNull(cache)) {
            cache = new HashMap<>();
            LOCAL.set(cache);
        }
        return cache.get(key);
    }

    public static void setTarget(String target) {
        set(Constant.Context.TARGET, target);
    }

    public static String getTarget() {
        Object object = get(Constant.Context.TARGET);
        return Objects.isNull(object) ? null : (String) object;
    }

    public static Long getSubject() {
        Object object = get(Constant.Context.SUBJECT);
        return Objects.isNull(object) ? null : (long) object;
    }

    public static void setSubject(Long subject) {
        set(Constant.Context.SUBJECT, subject);
    }

    public static void remove() {
        LOCAL.remove();
    }

}
